<?php
namespace app\controller;

use app\validate\Version as VersionValidate;
use think\exception\ValidateException;

class Version extends Base
{
    protected $middleware = ['checkAdmin'];

    // 软件列表默认显示数量
    protected $software_count = 50;

    protected function initialize()
    {
        parent::initialize();
        $this->logic = new \app\logic\Version();
    }

    /**
     * 获取版本分页列表
     */
    public function index()
    {
        if(request()->isAjax()) {
            $param = request()->param(['sid', 'status', 'page', 'limit'], [], 'trim');
            // 验证数据
            try {
                validate(VersionValidate::class)->scene('search')->check($param);
            } catch (ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }

            return $this->jsonReturn($this->logic->getList($param));
        }

        return view("", [
            // 获取软件列表
            'softList'  => (new \app\logic\Software())->getList(['limit' => $this->software_count], "id,name,config")[2] ?? [],
        ]);
    }

    /**
     * 新增
     */
    public function add()
    {
        if(request()->isAjax()) {
            $param = request()->param(['sid', 'config', 'status'], [], 'trim');
            // 验证数据
            try {
                validate(VersionValidate::class)->scene('add')->check($param);
            } catch (ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }

            return $this->jsonReturn($this->logic->add($param));
        }

        return view("", [
            // 获取软件列表
            'softList'  => (new \app\logic\Software())->getList(['limit' => $this->software_count], "id, name")[2] ?? [],
        ]);
    }

    /**
     * 修改
     */
    public function edit(int $id = 0)
    {
        if(request()->isAjax()) {
            $param = request()->param(['id', 'sid', 'config', 'status'], [], 'trim');
            // 验证数据
            try {
                validate(VersionValidate::class)->scene('edit')->check($param);
            } catch (ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }

            return $this->jsonReturn($this->logic->edit($param));
        }
        return view("", [
            'info'  => $this->logic->getInfo(['id' => intval($id)], "id,sid,config")[2] ?? []
        ]);
    }

    /**
     * 获取软件信息
     */
    public function getSoftwareInfo()
    {
        if(request()->isAjax()) {
            $id = request()->param('id', "", "trim,intval");

            $result = (new \app\logic\Software())->getInfo(['id' => $id], "id,config");
            if ($result) {
                return $this->jsonReturn(config('status.success'), "ok", $result);
            }
            return $this->jsonReturn(config('status.noData'), "信息不存在");
        }
    }

    /**
     * 删除
     */
    public function delete()
    {
        $param = request()->param(['ids'], [], 'trim,strip_tags');
        // 验证数据
        try {
            validate(VersionValidate::class)->scene('delete')->check($param);
        } catch (ValidateException $e) {
            return $this->jsonReturn(config('status.verify'), $e->getError());
        }

        return $this->jsonReturn($this->logic->delete($param['ids']));
    }

    /**
     * 设置状态
     */
    public function setStatus()
    {
        if(request()->isAjax()) {
            $id = request()->param('id', "", 'trim,intval');
            return $this->jsonReturn($this->logic->setStatus($id));
        }
    }
}